package com.example.demo.controller;

import com.example.demo.pojo.Accounts.FundAccounts;
import com.example.demo.pojo.R;
import com.example.demo.pojo.Stock.StockPositions;
import com.example.demo.service.StockInfoService;
import com.example.demo.utils.TokenUtil.JwtUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/stockInfo")
public class StockInfoController {
    private static final Logger logger = LoggerFactory.getLogger(StockInfoController.class);

    @Resource
    private StockInfoService stockInfoService;

    /**
     * 传入某个人的id,然后查询他的所有持仓信息,再根据股票代码来查询最新的数据并进行计算
     */
    @GetMapping("/GetDynamicInformation")
    public R<List<StockPositions>> GetDynamicInformation(@CookieValue(name = "token", required = false) String token) {
        if (token == null || token.isEmpty() || !JwtUtils.verifyToken(token)) {
            logger.warn("用户未登录或Token无效");
            return R.failed("用户未登录或登录异常");
        }

        try {
            return stockInfoService.GetDynamicInformation(token);
        } catch (Exception e) {
            logger.error("查询失败", e);
            return R.failed("查询失败");
        }
    }

    //获取个人总的动态信息
    @GetMapping("/GetAllDynamicInformation")
    public R<FundAccounts> GetAllDynamicInformation(@CookieValue(name = "token", required = false) String token) {
        if (token == null || token.isEmpty() || !JwtUtils.verifyToken(token)) {
            logger.warn("用户未登录或Token无效");
            return R.failed("用户未登录或登录异常");
        }

        try {
            return stockInfoService.GetAllDynamicInformation(token);
        } catch (Exception e) {
            logger.error("查询失败", e);
            return R.failed("查询失败");
        }
    }
}
